package cn.com.greatwall.flowable.service.mapstruct;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.flowable.engine.repository.Model;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.com.greatwall.common.base.BaseMapper;
import cn.com.greatwall.flowable.service.dto.ModelDto;

/**
 * @Author herw
 * @Time 2020-11-16 14:20:45
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@Component
public class ModelMapper implements BaseMapper<ModelDto, Model> {

    @Override
    public Model toEntity(ModelDto dto) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ModelDto toDto(Model entity) {
        ModelDto dto = new ModelDto();
        dto.setCategory(entity.getCategory());
        dto.setCreateTime(entity.getCreateTime());
        dto.setId(entity.getId());
        dto.setKey(entity.getKey());
        dto.setLastUpdateTime(entity.getLastUpdateTime());
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode node = mapper.readTree(entity.getMetaInfo());
            dto.setDescription(node.get("description").asText());
        } catch (IOException e) {
            e.printStackTrace();
        }
        dto.setName(entity.getName());
        dto.setVersion(entity.getVersion());
        dto.setDeployed(entity.getDeploymentId() != null ? true : false);
        dto.setTenantId(entity.getTenantId());
        
        return dto;
    }

    @Override
    public List<Model> toEntity(List<ModelDto> dtoList) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<ModelDto> toDto(List<Model> entityList) {
        List<ModelDto> dtoList = new ArrayList<>();
        for (Model instance : entityList) {
            dtoList.add(toDto(instance));
        }
        return dtoList;
    }

}
